﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using CkSoftware.GroupMe.Sdk.Helpers.Authorization;
using CkSoftware.GroupMeChatModule.Components;
using CkSoftware.GroupMeChatModule.Managers;
using DotNetNuke.Web.Api;

namespace CkSoftware.GroupMeChatModule.WebApi
{
	public class GroupMeAuthController : DnnApiController
	{
		[AllowAnonymous]
		[HttpGet]
		public HttpResponseMessage Callback(string access_token)
		{
			if (!string.IsNullOrEmpty(access_token))
			{
				if (UserInfo.UserID > 0)
				{
					GroupMeAuthManager.Instance.SetAuthTokenForUser(UserInfo.UserID, access_token);
				}
				else
				{
					GroupMeAuthManager.Instance.SetAuthTokenForAnonymous(SessionIdProvider.GetSessionId(), access_token);
				}
			}

			return new HttpResponseMessage {StatusCode = HttpStatusCode.Redirect, Headers = {{"location", "/"}}};
		}
	}
}